#include "register.h"
#include "ui_register.h"
#include "login.h"
#include "network.h"
#include <string>
#include <QApplication>
#include <QKeyEvent>
#include "buddylist.h"

Register::Register(QWidget *parent) :
        QDialog(parent),
        m_ui(new Ui::Register)
{
    m_ui->setupUi(this);
    this->setWindowTitle("Register");
    connect(m_ui->registerButton, SIGNAL(clicked()), this, SLOT(sendInfo()));
    connect(m_ui->backButton, SIGNAL(clicked()), this, SLOT(backToLogin()));
    connect( ClientHandler::clientHandlerInstance(), SIGNAL(registrationPassed()), this, SLOT(regApproved()));
    connect( ClientHandler::clientHandlerInstance(), SIGNAL(registrationDenied()), this, SLOT(statusLabelUpdate()));

}
Register::~Register()
{
    delete m_ui;
}

void Register::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Register::sendInfo()
{
    if (comparePass())
    {
        ClientHandler::clientHandlerInstance()->setUsername(setUsername());
        ClientHandler::clientHandlerInstance()->setPassword(setPassword());
        ClientHandler::clientHandlerInstance()->setEmail(setEmail());
        Network::networkInstance()->createConnection();
        ClientHandler::clientHandlerInstance()->setClientSocket(&Network::networkInstance()->tcpSocket);
        ClientHandler::clientHandlerInstance()->sendRegistration();

        //        connect( ClientHandler::clientHandlerInstance(), SIGNAL(authorizationDenied()), this, SLOT(statusLabelUpdate(2)));
    }

}

bool Register::comparePass()
{
    if (setPassword().compare(setCompare()) != 0)
    {
        statusLabelUpdate();
        ClientHandler::clientHandlerInstance()->setResult(-2);
        return false;
    }
    return true;
}

void Register::statusLabelUpdate()
{
    m_ui->errorLabel->show();
    switch (ClientHandler::clientHandlerInstance()->getResult()) {
    case -1: m_ui->errorLabel->setText(tr("<FONT COLOR=red>That user name or e-mail already exists. Please try again.</FONT>"));break;
    case -2: m_ui->errorLabel->setText(tr("<FONT COLOR=red>Your passwords don't match. Please try again.</FONT>"));break;
    case -3: m_ui->errorLabel->setText(tr("<FONT COLOR=red>All fields must be completed.</FONT>"));break;
    default: m_ui->errorLabel->setText(tr("<FONT COLOR=red>***Error*** Please try again.</FONT>"));
    }

}

QString Register::setUsername()
{
    return m_ui->newName->text();
}

QString Register::setPassword()
{
    return m_ui->newPassword->text();
}

QString Register::setEmail()
{
    return m_ui->newEmail->text();
}

QString Register::setCompare()
{
    return m_ui->confirmPassword->text();
}

//void Register::regApproved()
//{
//    this->close();
//    Login* loginWindow_p = new Login(this);
//    loginWindow_p->show();
//}

void Register::regApproved()
{
    this->close();
    BuddyList *buddyListWindow_p = new BuddyList(this);
    buddyListWindow_p->show();
}

void Register::backToLogin()
{
    this->close();
    Login *loginWindow_p = new Login(this);
    loginWindow_p->show();
}

void Register::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Return)
    {
        sendInfo();
    }
}

